package com.dlc.felear.xiaoensale.dao;

import android.content.Context;
import android.util.Log;

import com.dlc.felear.xiaoensale.entity.FoodMaterial;
import com.dlc.felear.xiaoensale.helper.JsonHelper;
import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FoodMaterialDao {
    private static final String TAG="FoodMaterialDao";
    private Dao<FoodMaterial, Integer> PipeInfoDao;

    /**
     * 构造方法
     * 获得数据库帮助类实例，通过传入Class对象得到相应的Dao
     * @param context
     */
    public FoodMaterialDao(Context context) {
        try {
            PipeInfoDao = DetailDataOpenHelper.getInstance(context).getDao(FoodMaterial.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }



    public void addAll(List<FoodMaterial> list){
        if(list==null){
            return;
        }
        for(FoodMaterial foodMaterial:list){
            add(foodMaterial);
        }
    }

    public void add(FoodMaterial foodMaterial){
        try {
            PipeInfoDao.createOrUpdate(foodMaterial);
        }catch (Exception e){

        }
    }


    /**
     * 删除一条记录
     * @param PipeInfo
     */
    public void delete(FoodMaterial PipeInfo) {
        try {
            int result=PipeInfoDao.delete(PipeInfo);
            Log.e(TAG,"删除："+JsonHelper.toJson(PipeInfo)+" result="+result);
        } catch (SQLException e) {
            Log.e(TAG,"e="+e.getMessage());
            e.printStackTrace();
        }
    }
    public void deleteForCid(String cid) {
        try {
            List<FoodMaterial> ftemp= queryForAllByCId(cid);
            if(ftemp!=null){
              for(FoodMaterial foodMaterial:ftemp){
                  delete(foodMaterial);
              }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 查询所有记录
     * @return
     */
    public List<FoodMaterial> queryForAll() {
        List<FoodMaterial> PipeInfos = new ArrayList();
        try {
            PipeInfos = PipeInfoDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return PipeInfos;
    }

    public int getFoodIdByName(String name){
        List<FoodMaterial> PipeInfos;
        try {
            PipeInfos = PipeInfoDao.queryBuilder().where().eq("foodName",name).query();
            if(PipeInfos!=null&&!PipeInfos.isEmpty()){
                return PipeInfos.get(0).getFoodid();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 查询所有记录
     * @return
     */
    public List<FoodMaterial> queryForAllByName(String action) {
        List<FoodMaterial> PipeInfos = new ArrayList();
        try {
            PipeInfos = PipeInfoDao.queryBuilder().where().like("foodName","%"+action+"%").query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return PipeInfos;
    }
    public List<FoodMaterial> queryForAllByCId(String action) {
        List<FoodMaterial> PipeInfos = new ArrayList();
        try {
            PipeInfos = PipeInfoDao.queryBuilder().where().eq("cid",action).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return PipeInfos;
    }

    private FoodMaterial getFoodMaterialByCidNum(String cid,int num){
        List<FoodMaterial> PipeInfos = new ArrayList();
        try {
            PipeInfos = PipeInfoDao.queryBuilder()
                    .where().eq("cid",cid)
                    .and()
                    .eq("number",num)
                    .query();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
        if(PipeInfos==null|| PipeInfos.isEmpty()){
            return null;
        }
        return PipeInfos.get(0);
    }

}
